package com.ohuang.util_a.processPhoenix;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;




public class AutomaticRestartActivity {

    /**
     *
     * 应用进程被意外杀掉,自动重启app防止没有初始化闪退  多进程应用记得判断进程
     */
    public static void automaticRestartActivity(Application application,String activityClassName) {
        if (ProcessPhoenix.isPhoenixProcess(application)){
            return;
        }
        final boolean[] isCreate = {false};
        application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {


            @Override
            public void onActivityCreated( Activity activity,  Bundle savedInstanceState) {
                if (activity.getClass().getName().equals(activityClassName)){
                    isCreate[0] =true;
                }else {
                    if (!isCreate[0]){
                        ProcessPhoenix.triggerRebirth(activity);
                    }
                }
            }

            @Override
            public void onActivityStarted( Activity activity) {

            }

            @Override
            public void onActivityResumed( Activity activity) {

            }

            @Override
            public void onActivityPaused( Activity activity) {

            }

            @Override
            public void onActivityStopped( Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState( Activity activity,  Bundle outState) {

            }

            @Override
            public void onActivityDestroyed( Activity activity) {

            }
        });
    }
}
